<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="商家名字" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入商家名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家手机号" prop="companyPhone">
        <el-input
          v-model="queryParams.companyPhone"
          placeholder="请输入商家手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属分类" prop="companyTypeIds">
        <el-cascader
          v-model="companyTypeIds"
          :options="selectList"
          :show-all-levels="false"
          :props="{ label: 'label', value: 'value' }"
          @change="getAttributeData"
        ></el-cascader>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['advertising:add']"
          >新增</el-button
        >
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:user:remove']"
            >批量删除</el-button
          >
        </el-col>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="postList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" width="55" prop="id" />
      <el-table-column
        label="一级分类"
        width="140"
        align="center"
        prop="companyTypeName1"
        show-overflow-tooltip
      />
      <el-table-column
        label="二级分类"
        width="140"
        align="center"
        prop="companyTypeName2"
        show-overflow-tooltip

      />
      <el-table-column
        label="商家名字"
        width="240"
        align="center"
        prop="companyName"
      />
      <el-table-column
        label="主营"
        width="240"
        align="center"
        prop="companyBusiness"
      />
      <el-table-column
        label="手机"
        width="140"
        align="center"
        prop="companyPhone"
      />
      <el-table-column
        label="地址"
        width="240"
        align="center"
        prop="companyAddress"
        show-overflow-tooltip
      />
      <el-table-column
        label="图片"
        align="center"
        width="140"
        prop="companyHead"
      >
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.companyHead"
            class="img"
            :src="scope.row.companyHead"
            :preview-src-list="[scope.row.companyHead]"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column
        label="视频"
        align="center"
        width="140"
        prop="companyVideo"
      >
        <template slot-scope="scope">
          <video
            class="videos"
            controls
            preload="auto"
            playsinline="true"
            :src="scope.row.companyVideo"
          ></video>
        </template>
      </el-table-column>

      <el-table-column
        label="图文内容"
        width="240"
        align="center"
        prop="companyContent"
        show-overflow-tooltip
      />
      <el-table-column label="排序" width="60" align="center" prop="sort" />
      <el-table-column
        label="创建时间"
        align="center"
        width="180"
        prop="createTime"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="180"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['advertising:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['advertising:removeList']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属分类" prop="companyTypeIds">
              <el-cascader
                v-model="companyTypeIds"
                :options="selectList"
                :show-all-levels="false"
                :props="{ label: 'label', value: 'value' }"
                @change="getAttributeData"
              ></el-cascader>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="商家名字" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入商家名字" />
        </el-form-item>
        <el-form-item label="主营" prop="companyBusiness">
          <el-input v-model="form.companyBusiness" placeholder="请输入主营" />
        </el-form-item>
        <el-form-item label="手机号" prop="companyPhone">
          <el-input
            maxlength="11"
            v-model="form.companyPhone"
            placeholder="请输入手机号"
          />
        </el-form-item>
        <el-form-item label="地址" prop="companyAddress">
          <el-input v-model="form.companyAddress" placeholder="请输入地址" />
        </el-form-item>
        <el-row>
          <el-col :span="8">
            <el-form-item label="封面图" prop="companyHead">
              <upimg
                :up-with="140"
                :upimgUrl="form.companyHead"
                :up-height="160"
                :up-bradius="10"
                uptext="请上传封面图"
                @changeName="imgUrlfun3"
              ></upimg>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="视频" prop="companyVideo">
              <upimg
                :utype="2"
                uptext="上传视频"
                :upimg-url="form.companyVideo"
                :up-with="280"
                :up-height="160"
                :up-bradius="6"
                @changeName="imgUrlfun2"
              ></upimg>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="内容" prop="companyContent">
          <Editor v-model="form.companyContent"></Editor>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getList,
  addList,
  removeList,
  editList,
  listInfo,
  selectList,
} from "@/api/zhao/store";
import Upimg from "../../../components/UploadImg";
export default {
  name: "Post",
  components: { Upimg },
  data() {
    const validatePass = (rule, value, callback) => {
      if (this.form.companyTypeId1 && this.form.companyTypeId2) {
        callback();
      } else {
        callback(new Error("请选择分类"));
      }
    };
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: null,
        companyPhone: null,
        companyTypeId1: null,
        companyTypeId2: null,
      },
      // 表单参数
      form: {},
      companyTypeIds: [],
      // 表单校验
      rules: {
        companyTypeIds: [
          { required: true, trigger: "blur", validator: validatePass },
        ],
        companyName: [
          { required: true, message: "商家名字不能为空", trigger: "change" },
        ],
        companyBusiness: [
          { required: true, message: "主营不能为空", trigger: "change" },
        ],
        companyPhone: [
          { required: true, message: "手机号不能为空", trigger: "change" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号",
            trigger: "blur",
          },
        ],
        companyAddress: [
          { required: true, message: "地址不能为空", trigger: "change" },
        ],
        companyHead: [
          { required: true, message: "图片不能为空", trigger: "change" },
        ],
        companyContent: [
          { required: true, message: "图文内容不能为空", trigger: "change" },
        ],
      },
      isshow: false,
      swtype: [
        {
          id: 1,
          label: "是",
          value: 1,
        },
        {
          id: 2,
          label: "否",
          value: 0,
        },
      ],
      selectList: [],
    };
  },
  created() {
    this.getList();
    this.getselsectList();
  },
  methods: {
    getAttributeData(e) {
      this.form.companyTypeId1 = e[0];
      this.form.companyTypeId2 = e[1];
      this.queryParams.companyTypeId1 = e[0];
      this.queryParams.companyTypeId2 = e[1];
    },
    imgUrlfun2(e) {
      if (e) {
        this.form.companyVideo = e.path;
      } else {
        this.form.companyVideo = "";
      }
    },
    imgUrlfun3(e) {
      if (e) {
        this.form.companyHead = e.path;
      } else {
        this.form.companyHead = "";
      }
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true
      getList(this.queryParams).then((response) => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getselsectList() {
      selectList().then((response) => {
        if (response.code == 200) {
          let treeDetail = [];
          treeDetail = response.data;
          treeDetail.forEach((item) => {
            this.selectList.push(this.getModuleObject(item));
          });
        }
      });
    },
    getModuleObject(obj) {
      if (!obj.childrenList)
        return {
          id: obj.id,
          label: obj.typeName,
          value: obj.id,
          children: null,
        };
      const temp = {
        id: obj.id,
        label: obj.typeName,
        value: obj.id,
        children: [],
      };
      obj.childrenList.forEach((item) => {
        temp.children.push(this.getModuleObject(item));
      });
      return temp;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.companyTypeIds = [];
      this.queryParams.companyTypeId1 = "";
      this.queryParams.companyTypeId2 = "";
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.isshow = false;
      this.companyTypeIds = [];
      this.open = true;
      this.title = "添加商家";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.companyTypeIds = [];
      this.title = "修改商家";
      this.isshow = true;
      listInfo({ id: row.id }).then((res) => {
        this.form = res.data;
        this.companyTypeIds.push(this.form.companyTypeId1);
        this.companyTypeIds.push(this.form.companyTypeId2);
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.isshow) {
            editList(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log(this.form);
            addList(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.companyTypeIds = [];
              this.queryParams.companyTypeId1 = "";
              this.queryParams.companyTypeId2 = "";
              this.getList();
            });
          }
        }
      });
    },
    handleDelete(row) {
      let userIds = row.id || this.ids;
      if (typeof userIds == "number") {
        userIds = [userIds];
      }
      this.$modal
        .confirm('是否确认删除编号为"' + userIds + '"的数据项？')
        .then(function () {
          return removeList({ ids: userIds });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/post/export",
        {
          ...this.queryParams,
        },
        `post_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style>
.img {
  width: 120px;
  height: 80px;
  border-radius: 10px;
}
.videos {
  width: 120px;
  height: 80px;
  border-radius: 10px;
}
</style>
